package world.criver.h50529.function;

import world.criver.h50529.module.moEvent;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class funUdp {
    public String send(moEvent event){
        try {
            funMessageFormat mf=new funMessageFormat();
            DatagramSocket socket = new DatagramSocket();
            byte[] bs;
            if(event.getMessage_type().equalsIgnoreCase("hex"))bs=mf.hexToByte(event.getSend_data());
            else bs=mf.hexToByte(mf.stringToHex(event.getSend_data()));
            DatagramPacket packet=new DatagramPacket(bs,bs.length,InetAddress.getByName(event.getIp()),event.getPort());
            socket.send(packet);
            socket.close();
            return "已发送"+bs.length+"字节。";
        }catch (Exception e){
            e.printStackTrace();
            return event.getCommunication_mode()+"连接失败";
        }
    }
}
